<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/2
 * Time: 16:57
 */

require_once 'db.php';
require_once 'response.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$title = isset($_POST['title'])? $_POST['title'] : '';
$program = isset($_POST['program']) ? $_POST['program'] : '';
$match = isset($_POST['match']) ? $_POST['match'] : '';
$target = isset($_POST['target']) ? $_POST['target'] : '';
$service = isset($_POST['service']) ? $_POST['service'] : '';
$maker = isset($_POST['maker']) ? $_POST['maker'] : '';
$time = time();

if(empty($title) || empty($program) || empty($match) || empty($target) || empty($maker)) {
    return Response::show(401,'数据不合法');
}

try{
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$sql = "INSERT report VALUES(null,?,?,?,?,?,?,$time,0)";

$mysqli_stmt = $connect->prepare($sql);
$mysqli_stmt->bind_param('ssssss',$title,$program,$match,$target,$service,$maker);

$data = array(
    'IsSuccess' => false,
    'InsertId' => ''
);

if($mysqli_stmt->execute()) {
    $data['IsSuccess'] = true;
    $data['InsertId'] = $mysqli_stmt->insert_id;
    return Response::show(200,'月报上传成功',$data);
} else {
    //插入语句如果出现了error，execute的返回值就是false，就会执行else里面的内容
//    var_dump($mysqli_stmt);
    if($mysqli_stmt->errno == 1452) {
        return Response::show(400,'没有找到该用户',$data);
    } else {
        return Response::show(404,'月报上传失败,服务器没响应',$data);
    }
}




